package com.onlyphoto.onlymall.mvp.view.binder

import android.annotation.SuppressLint
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

import com.onlyphoto.onlymall.R
import com.onlyphoto.onlymall.bean.response.AddressBean
import com.onlyphoto.onlymall.mvp.view.activity.EditAddressActivity
import com.onlyphoto.onlymall.mvp.view.activity.ShippingAddressActivity
import com.onlyphoto.onlymall.mvp.view.activity.ShippingAddressActivity.Companion.REQUEST_ORDER_CONFIRM

import me.drakeet.multitype.ItemViewBinder

/**
 * Created by gaochujia on 2019-07-09.
 */
class AddressBinder : ItemViewBinder<AddressBean, AddressBinder.ViewHolder>() {

    private var isOrderConfirm = false

    override fun onCreateViewHolder(inflater: LayoutInflater, parent: ViewGroup): ViewHolder {
        val root = inflater.inflate(R.layout.item_address, parent, false)
        return ViewHolder(root)
    }

    override fun onBindViewHolder(viewHolder: ViewHolder, bean: AddressBean) {
        viewHolder.update(bean)
        viewHolder.itemView.setOnClickListener{
            listener?.callBack(bean)
        }
        viewHolder.ivEdit?.setOnClickListener{
            if (!isOrderConfirm) {
                viewHolder.ivEdit?.context?.startActivity(Intent(viewHolder.ivEdit?.context, EditAddressActivity::class.java)
                        .putExtra("addressBean", bean))
            } else {
                (viewHolder.ivEdit?.context as ShippingAddressActivity).startActivityForResult(Intent(viewHolder.ivEdit?.context, EditAddressActivity::class.java)
                        .putExtra("addressBean", bean)
                        .putExtra("isOrderConfirm", isOrderConfirm), REQUEST_ORDER_CONFIRM)
            }
        }
    }

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        var name: TextView? = null
        var phone: TextView? = null
        var address: TextView? = null
        var isDefault: TextView? = null
        var ivEdit: ImageView? = null

        init {
            name = itemView.findViewById(R.id.name)
            phone = itemView.findViewById(R.id.phone)
            address = itemView.findViewById(R.id.address)
            isDefault = itemView.findViewById(R.id.is_default)
            ivEdit = itemView.findViewById(R.id.iv_edit)
        }

        @SuppressLint("SetTextI18n")
        fun update(bean: AddressBean) {
            name?.text = bean.name
            phone?.text = bean.phone
            address?.text = bean.province + bean.city + bean.district + bean.place
            if (bean.state == 1) {
                isDefault?.visibility = View.VISIBLE
            } else {
                isDefault?.visibility = View.GONE

            }
        }
    }

    fun isOrderConfirm(isOrderConfirm: Boolean){
        this.isOrderConfirm = isOrderConfirm
    }

    var listener: ResultListener? = null

    fun setResultListener(listener: ResultListener) {
        this.listener = listener
    }

    interface ResultListener{
        fun callBack(bean: AddressBean)
    }
}
